<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$uniacid=$_SESSION['uniacid'];
$mid=$_SESSION['uid'];
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['id']);
	$ordersn=$_RQ['ordersn'];
	if (empty($ordersn)) {
		exi('请扫码核销码!','error','jy_quanhuo/merch/');
	}
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$order=pdo_get('jy_merch_game_order',array('uniacid'=>$uniacid,'ordersn'=>$ordersn));
	if(empty($order)){
		exi($ordersn.'核销码错误,无权核销','error','jy_quanhuo/merch/');   
	}
	if(!$order['merchid']){
		exi('非商户订单,无权核销','error','jy_quanhuo/merch/');
	}
	//核销店员身份
	$prams=array('status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$prams['merchid']=$order['merchid'];
	$store=model('jy_merch/verify')->checkuser($_SESSION['uid'],$order['merchid'],'jy_quanhuo/merch/');
	if($order['status']==0 || $order['status']==4){
		exi('订单未付款或已关闭!','error','jy_quanhuo/merch/');
	}
	if($order['status']==4){
		exi('订单已关闭，不能核销！','error','jy_quanhuo/merch/');
	}elseif($order['status']==5){
		exi('订单已申请退款，不能核销！','error','jy_quanhuo/merch');
	}elseif($order['status']==5){
		exi('订单已退款，不能核销！','error','jy_quanhuo/merch');
	}
	$order_goods=json_decode($order['goods'],true);
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && $order['address']['name']){
		//$order['address']=$order['address'];
	}else{
		$order['address']=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	if(!is_array($order['address']['pcd'])){
		$order['address']['pcd']=json_decode($order['address']['pcd'],true);
	}
	$order['sendtypetitle']=$this->sendtypes[$order['sendtype']];
	if($order['tihuo_id']){
		$order['tihuo']=pdo_get('jy_merch_tihuo',array('id'=>$order['tihuo_id'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		$order['tihuo']=pdo_get('jy_merch_tihuo',array('merchid'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']));
	}
	$order['tihuo']['pcd']=json_decode($order['tihuo']['pcd'],true);
	$return['post']=array(
		'orderid'=>$order['id'],
		'ordersn'=>$ordersn,
	);
	$return['order']=$order;
	$return['order_goods']=$order_goods;
	$return['store']=$store;
	set_html('title','核销');
    exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	pdo_update('jy_merch_game_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	$order=pdo_get('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	model('jy_quanhuo/commission')->order_complete($order);
	exi('核销成功！','','jy_quanhuo/merch/');
	
}